<?php

namespace App\Http\Controllers\User;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Category;
use App\Models\Dashboard;
use App\Models\TaskReply;
use App\Models\User;
use App\Models\TaskProcess;
use App\Models\TaskComment;
use App\Models\TaskService;
use App\Models\Service;
use App\Models\Client;
use App\Models\ClientUser;
use App\Models\ClientCar;
use App\Repos\DashboardRepo;
use App\Repos\DepartRepo;
use App\Models\MaintainType;
use App\Models\ServiceType;
use Session;
use DB,Log;
use App\Services\Wechat;
use App\Services\Msgtpl;
use Image;

class DashboardController extends Controller
{
    public function index( Request $request)
    {
        $wx_user =  session('wechat.oauth_user.default');
        //dd($wx_user);
        $where   = $param = [];
        //$client_ids  = ClientUser::where(['openid'=>$wx_user->id])->pluck('client_id');
        
        $query   = Dashboard::query();
        
        if($request->keyword) {
            $car_num = $param['keyword'] = $request->keyword; 
            $query->where('car_num','like','%'.$car_num.'%');
        }
        $param['status'] = 1;
        if($request->status==100) {
            $param['status'] = (int)$request->status; 
            $query->where('is_completed',1);            
        } else {
            $query->where('is_completed',0);
        }
      
        $list = $query->orderBy('level','desc')
                        ->orderBy('id','desc')
                        ->paginate(20);
        
                
        $levelArr   = DashboardRepo::getLevelArr();  
        $levelCss   = DashboardRepo::getLevelCss();  
                      
        return view('4s.dashboard.list',compact('list','param','levelArr','levelCss'));
        
    }

    public function create()
    {     

        $wx_user    = session('wechat.oauth_user.default');
        $openid     = $wx_user->id;
        $worker     = User::where('openid',$openid)->first();
        if(!$worker) {
            $rs['msg']    = '没有权限';
            $rs['status'] = 'danger';
            return redirect('/user/task')->with('rs',$rs);
        }
        $expert = [];
        
        $levelArr   = DashboardRepo::getLevelArr();
        return view('4s.task-add',compact('data','levelArr'));
    }   

   

   public function process(Request $request)
    {
        
        $rs['status'] = 'danger';
        $rs['msg']    = '操作成功';  
        $data = $request->except(['_token']);     
        $flag = DashboardRepo::dispatch($data);
        if($flag == true) {
            $rs['status'] = 'success';
            $rs['msg'] = '操作成功';
            return back()->with('rs',$rs);
        } 
        return back()->with('rs',$rs);
        
    }

  


    public function show($id)
    {
        $data = Dashboard::withTrashed()->find($id);
       
       
        $wechat_user = session('wechat.oauth_user.default');
        $openid      = $wechat_user->id;
        $worker      = User::where('openid',$openid)->orderBy('role_code','asc')->first();
       
        $levelArr   = DashboardRepo::getLevelArr();
        $workers    = User::where('role_code',2)->get();

        return view('4s.dashboard.info',compact('data','worker','levelArr','workers'));
    }   

   
    
    /**
     * 完工
     */
    public function complete(Request $request)
    {
        $rs['status'] = 'danger';
        $rs['msg']    = '操作失败'; 

           
       
        $task   = Dashboard::find($request->dashboard_id);
        $task->is_completed    = 0;
        if($request->type==100) {


            $task->is_completed    = 1;
            $task->completed_at    = date('Y-m-d H:i:s');
        }
        $task->repair_contents = $request->repair_contents;
        $flag   = $task->save();        
        

        // 推送消息
        
            $msg      = $task->toArray();
            if($request->type==100) {
                $msg['first']  = '车辆完工通知';
               
                $msg['arrived_at'] = $task->in_date;
                $msg['url']    = config('app.url').'/user/dashboard/show/'.$task->id;

                // 通知管理员
                $users = User::where('role_code',1)->get();
                foreach ($users as $k => $v) {
                    if(empty($v->openid)) continue;
                    $msg['openid'] = $v->openid;
                    Wechat::sendMsgTpl('complete_to_client&driver',$msg);  
                }    
            } else {
                $msg['first']  = '维修进度通知';
               
                $msg['maintain_name'] = $task->status;
                $msg['status_txt'] = $task->repair_contents;
                $msg['url']    = config('app.url').'/user/dashboard/show/'.$task->id;

                // 通知管理员
                $users = User::where('role_code',1)->get();
                foreach ($users as $k => $v) {
                    if(empty($v->openid)) continue;
                    $msg['openid'] = $v->openid;
                    Wechat::sendMsgTpl('process',$msg);  
                }  
            }
            
            
        
        
        
        

        if($flag == true) {
            $rs['status'] = 'success';
            $rs['msg'] = '操作成功';
            return back()->with('rs',$rs);
        } 
        return back()->with('rs',$rs);
        
    }



    


}
